package com.java.control;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.java.common.SessionInfo;
import com.java.entity.User;
import com.java.mis.LoginMessage;
import com.java.mis.UserType;
import com.java.service.BaseService;



public class AuthorizationCredit extends BaseService{

	public final String SESSION_NAME = "memberSessionInfo";
	private Object sessionSetting;
	
	public LoginMessage login(HttpServletRequest request,String sign,String pass){
		User user = this.dao.loadObject(User.class, "account=?", sign);
		//用户未找到
		if(null==user){
			return LoginMessage.NOFOUND;
		}
		//密码错误
		if(!user.getPassword().equals(pass)){
			return LoginMessage.ERROR;
		}
		//账号被禁用
		if(!user.getStatus()){
			return LoginMessage.LIMIT;
		}
		
		SessionInfo sessionInfo = new SessionInfo();
		sessionInfo.setUserId(user.getId());
		sessionInfo.setAccount(user.getAccount());
		sessionInfo.setUserName(user.getUserName());
		sessionInfo.setUserType(user.getUserType());
//		sessionInfo.setIsAdmin(user.getIsAdmin());
		sessionInfo.setIsDeptAdmin(user.getIsDeptAdmin());
//		if(user.getRole()!=null){
//			sessionInfo.setRole(user.getRole().getRoleName());
//			sessionInfo.setAcl(user.getRole().getOpAcl());
//		}
		request.getSession().setAttribute(SESSION_NAME, sessionInfo);
		
		return null;
	}
	
	public void loginout(HttpServletRequest request){
		request.getSession().removeAttribute(SESSION_NAME);		
		request.getSession().invalidate();	
	}
	
	
	public SessionInfo getSessionInfo(HttpServletRequest request) {

		SessionInfo sessionObj = (SessionInfo) request.getSession().getAttribute(SESSION_NAME);

		if (null == sessionObj)
			return null;
		else
			return sessionObj;
	}

	public boolean signinValidate(HttpServletRequest request) throws Exception {

		if (request.getSession().getAttribute(SESSION_NAME) != null)
			return true;
		else
			return false;
	}

	/**
	 * 检查后台用户是否有页面URL权限
	 * @param request
	 * @return
	 */
//	public boolean checkAuthenticationUrl(HttpServletRequest request){
//		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(this.SESSION_NAME);
//		if(sessionInfo!=null){
//			if(sessionInfo.getUserType()==UserType.admin){
//				return true;
//			}
//			return true;
//			if(StringUtils.isBlank(sessionInfo.getAcl())){
//				return false;
//			}
//			String url = request.getRequestURI();
//			String acl = sessionInfo.getAcl();
//			
//			String[] mods = acl.split("|");
//			for(int i=0;i<mods.length;i++){
//				String[] funs = mods[i].split(";");
//				if(funs!=null&& funs.length>0){
//					if(url.contains(funs[0])){
//						return true;
//					}	
//				}					
//			}
//		}
//		return false;
//	}
	
	public void redirectSigninValidateFailView(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String failView = this.signinValidateFailView;
		String contextPath = request.getContextPath();

		if (!contextPath.equals("")) {
			if (!contextPath.substring(contextPath.length() - 1).equals("/")) {
				contextPath += "/";
			}
		} else {
			contextPath = "/";
		}
		failView = failView.replaceAll("~/", contextPath);

		response.sendRedirect(failView);
		
	}

	private String signinValidateFailView;
	
	public void setSigninValidateFailView(String signinValidateFailView) {
		this.signinValidateFailView = signinValidateFailView;
	}

}
